package com.leyou.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.auth.dto.Payload;
import com.leyou.auth.dto.UserDetail;
import com.leyou.auth.utils.JwtUtils;
import com.leyou.common.exception.LyException;
import com.leyou.user.dto.UserDTO;
import com.leyou.user.entity.User;
import com.leyou.user.mapper.UserMapper;
import com.leyou.user.service.UserService;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.leyou.common.constants.MQConstants.ExchangeConstants.SMS_EXCHANGE_NAME;
import static com.leyou.common.constants.MQConstants.RoutingKeyConstants.VERIFY_CODE_KEY;


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    private static final String KEY_PREFIX_Code = "code:";

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @Autowired
    private JwtUtils jwtUtils;

    @Override
    public UserDTO queryUserByUsernameAndPassword(String username, String password) {
        User user = this.query().eq("username", username).one();
        if(user == null || !passwordEncoder.matches(password,user.getPassword())){
            throw new LyException(400,"用户名或密码错误！");
        }

        return new UserDTO(user);
    }

    @Override
    public void registry(String username, String password, String phone, String code) {
        //首先验证验证码是否正确
        if(code == null || ! code.equals(redisTemplate.opsForValue().get(KEY_PREFIX_Code+phone))){
            throw new LyException(400,"验证码错误");
        }

        //注册用户
        User user = new User();
        user.setUsername(username);
        user.setPassword(passwordEncoder.encode(password));
        user.setPhone(phone);

        this.save(user);
    }

    @Override
    public void sendMessage(String phone) {

        //生成随机验证码
//        String code = RandomStringUtils.randomNumeric(6);

//        Map<String, String> msg = new HashMap<>();
//        msg.put("phone", phone);
//        msg.put("code", code);
//
//        amqpTemplate.convertAndSend(SMS_EXCHANGE_NAME,VERIFY_CODE_KEY,msg);

        Map<String, String> msg = new HashMap<>();
        msg.put("phone", phone);

        // 2.使用Apache的工具类生成6位数字验证码
        String code = RandomStringUtils.randomNumeric(6);
        msg.put("code", code);

        redisTemplate.opsForValue().set(KEY_PREFIX_Code + phone,code,5,TimeUnit.MINUTES);

        this.amqpTemplate.convertAndSend(SMS_EXCHANGE_NAME, VERIFY_CODE_KEY, msg);

    }

    @Override
    public Boolean isExists(String data, Integer type) {
        Integer count;

        switch (type) {
            case 1:
                 count = this.query().eq("username", data).count();
                break;
            case 2:
                count = this.query().eq("phone",data).count();
                break;
            default:
                throw new LyException(400,"请求参数有误");
        }

        return count == 1;
    }

}